package cn.weiron.fun.infrastructure.dictionary.repository;


import cn.weiron.fun.infrastructure.dictionary.Dictionary;
import cn.weiron.fun.infrastructure.dictionary.DictionaryGroup;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import javax.persistence.*;

/**
 * 仓储层：字典类MySQL数据管理
 */
@Repository("dictionaryMysqlRepository")
public interface DictionaryMysqlRepository extends JpaRepository<DictionaryMysqlRepository.DictionaryPo, String> {
    /**
     * 实体类
     */
    @Entity
    @Table(name = "dictionary")
    class DictionaryPo {
        /**
         * 字典编码
         */
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "code", unique = true, nullable = false, length = 20)
        private String code;

        /**
         * 自定对应的标识内容
         */
        @Column(name = "label", nullable = false)
        private String label;

        /**
         * 分组标识
         */
        @Column(name = "group",nullable = false)
        private String group;

        /**
         * 字典说明
         */
        @Column(name = "description")
        private String description;

        /**
         * 字典扩展信息，用于自定义配置
         */
        @Column(name = "extend")
        private String extend;

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getLabel() {
            return label;
        }

        public void setLabel(String label) {
            this.label = label;
        }

        public String getGroup() {
            return group;
        }

        public void setGroup(String group) {
            this.group = group;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getExtend() {
            return extend;
        }

        public void setExtend(String extend) {
            this.extend = extend;
        }
    }

    /**
     * 转换器
     */
    class Assembler {
        public static Dictionary to(DictionaryPo po) {
            return (new Dictionary())
                    .setCode(po.getCode())
                    .setLabel(po.getLabel())
                    .setGroup(DictionaryGroup.valueOf(po.getGroup()))
                    .setDescription(po.getDescription())
                    .setExtend(po.getExtend())
                    ;
        }

        public static DictionaryPo from(Dictionary curDo) {
            DictionaryPo po = new DictionaryPo();
            po.setCode(curDo.getCode());
            po.setLabel(curDo.getLabel());
            po.setGroup(curDo.getGroup().getValue());
            po.setDescription(curDo.getDescription());
            po.setExtend(curDo.getExtend());
            return po;
        }
    }
}